import 'package:flame/events.dart';
import 'package:flame/game.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';

import 'package:hero_back/study/flutter_game_flame/day01/monster.dart';

import 'bg.dart';

class TolyGame extends FlameGame with KeyboardEvents {
  @override
  Future<void> onLoad() async {
    final Vector2 fixSize = Vector2(500, 300);
    camera.viewport = FixedResolutionViewport(fixSize);
    // camera.setRelativeOffset(Anchor.bottomLeft);
    camera.speed = 100;
    add(BG());
    add(Monster()..position = size / 2);
  }

  @override
  void onGameResize(Vector2 canvasSize) {
    super.onGameResize(canvasSize);
    print(size);
  }

  @override
  KeyEventResult onKeyEvent(
      RawKeyEvent event, Set<LogicalKeyboardKey> keysPressed) {
    final isKeyDown = event is RawKeyDownEvent;
    if (event.logicalKey == LogicalKeyboardKey.keyZ && isKeyDown) {
      camera.zoom += 0.1;
    }
    if (event.logicalKey == LogicalKeyboardKey.keyX && isKeyDown) {
      camera.zoom -= 0.1;
    }
    if (event.logicalKey == LogicalKeyboardKey.arrowRight && isKeyDown) {
      camera.moveTo(Vector2(0, size.y / 2 + 37 / 2));
    }
    if (event.logicalKey == LogicalKeyboardKey.arrowUp && isKeyDown) {
      camera.moveTo(Vector2(0, size.y / 2 - 37 / 2));
    }
    return super.onKeyEvent(event, keysPressed);
  }
}
